package com.zmj.dao.impl;


import java.util.ArrayList;
import java.util.List;


import com.zmj.constant.CommSQL;
import com.zmj.dao.IGoodsDao;
import com.zmj.entity.Goods;
import com.zmj.entity.GoodsType;

public class GoodsDaoImpl extends BaseDaoImpl implements IGoodsDao{

	
	public List<Goods> selGoods(Goods goods) {
		String sql = CommSQL.GOODS_SELEC;
		
		List<Object> list = new ArrayList<Object>();
		
		//判断查询条件;
		//1:根据商品的类别来查找;
		if(goods.getGtype() != 0){//根据商品类别来查找    and gtype=? 
			sql += " and gtype=? ";
			list.add(goods.getGtype());
		}
		
		if(goods.getGnew() != 0){ //查找是否是新品的内容
			sql += " and gnew = ? ";
			list.add(goods.getGnew());
		}
		
		if(goods.getGdiscount() != 0){ //查找是否是折扣商品
			sql += " and gdiscount < 1 ";
		}
		
		if(goods.getStartprice() > 0){ //设置了起始的区间价格
			sql += " and gprice > ? ";
			list.add(goods.getStartprice());
		}
		
		//价格区间查询
		if(goods.getEndprice() > 0 && goods.getEndprice() > goods.getStartprice()){
			sql += " and gprice < ? ";
			list.add(goods.getEndprice());
		}
		
		//判断是否排序;
		
		//判断是否分页;
		if(null != goods && null != goods.getPage()){
			sql += CommSQL.LIMIT;
			list.add(goods.getPage().getStart());
			list.add(goods.getPage().getPageSize());
		}
		
		return sel(Goods.class, sql, list.toArray());
		
	}

	@Override
	public List<GoodsType> queryType() {
		String sql="select tid, tname, tdesc from goods_type ";
		return sel(GoodsType.class, sql);
	}

	@Override
	public Goods selOneGood(int gid) {
		String sql=CommSQL.GOODS_SELEC+" and gid= ?";
		return selOne(Goods.class, sql, gid);
	}

}
